package com.log4jviewer.ui.views;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class represents a handler for 'Error' button. It opens an error view to see occurred errors.
 * 
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public class ErrorViewAction implements IViewActionDelegate {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private LogView logView;

    @Override
    public void run(final IAction action) {
        ErrorView errorView = (ErrorView) logView.openView(ErrorView.ID);
        errorView.setErrorManager(logView.getErrorManager());
        errorView.showErrors();
        errorView.setFocus();
        logView.setErrorActionState(false);
    }

    @Override
    public void selectionChanged(final IAction action, final ISelection selection) {
        logger.debug("Error action: Selection Changed!!");

        // Update error action state after selection in log table
        boolean errorActionEnabled = logView.isErrorActionEnabled();
        logger.debug("Is error action enabled: {}", errorActionEnabled);
        logView.setErrorActionState(errorActionEnabled);
    }

    @Override
    public void init(final IViewPart view) {
        this.logView = (LogView) view;
    }
}
